package com.spsoft.system.service.impl;

import com.spsoft.system.mapper.SequenceMapper;
import com.spsoft.system.service.SequenceService;
import lombok.RequiredArgsConstructor;
import lombok.Synchronized;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : chenxudong
 * @date : 2024/1/3 10:47
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class SequenceServiceImpl implements SequenceService {

    private final SequenceMapper sequenceMapper;

    @Override
    @Transactional
    @Synchronized
    public Long getSequence(String tableName) {
        sequenceMapper.autoIncrementSequence(tableName);
        return sequenceMapper.getSequence(tableName);
    }

    @Override
    @Transactional
    @Synchronized
    public List<Long> getSequenceList(String tableName, int size) {
        List<Long> idList = new ArrayList<>();

        sequenceMapper.updateSequence(tableName, size);
        Long sequence = sequenceMapper.getSequence(tableName);
        for (int i = size -1 ; i >= 0; i--) {
            idList.add(sequence - i);
        }
        return idList;
    }
}
